<template>
  <div class="recommend-warp">
    <div class="tit td-fbc">
      <div class="td-fbc">
        <img :src="$global.Image.icon_shop" />
        <span>猜你喜欢</span>
      </div>
      <span class="more a" @click="$router.push('/likeGoods')">更多 ></span>
    </div>
    <div class="recommend-box">
      <div class="slide pos-r">
        <button type="button" class="el-carousel__arrow el-carousel__arrow--left" @click="prev">
          <i class="el-icon-arrow-left"></i>
        </button>
        <button type="button" class="el-carousel__arrow el-carousel__arrow--right" @click="next">
          <i class="el-icon-arrow-right"></i>
        </button>
        <el-carousel
          ref="carousel"
          arrow="never"
          height="360px"
          interval="5000"
        >
          <el-carousel-item v-for="(group, index) in hostProduct" :key="index">
            <div class="goods-recommend td-fbs">
              <div
                class="goods-box"
                v-for="(item, idx) in group.list"
                :key="idx"
                @click="_goGoodsDetail(item)"
              >
                <img class="goods-img" :src="item.image" />
                <div class="goods-name ellipsis" :title="item.store_name">{{ item.store_name }}</div>
                <div class="goods-price td-fbc">
                  <span>¥{{ item.price }}</span>
                  <img :src="$global.Image.icon_cart" @click.stop="_showGoodsInfo(item)" />
                </div>
              </div>
            </div>
          </el-carousel-item>
        </el-carousel>
      </div>
    </div>
    <!-- add cart -->
    <v-add-cart ref="addCart"></v-add-cart>
  </div>
</template>

<script>
import { getHostProducts } from "@api/store";
import { goShopDetail } from "@libs/order";
import addCart from "@components/addCart";

export default {
  name: "Recommend",
  components: {
    'v-add-cart': addCart
  },
  props: {
    type: {
      type: String,
      default: "hot"
    }
  },
  data() {
    return {
      hostProduct: [],
      page: 1,
      limit: 20,
      loadTitle: "",
      loading: false,
      loadend: false
    };
  },
  watch: {},
  mounted() {
    this.hostProducts();
  },
  methods: {
    hostProducts() {
      let that = this;
      if (that.loading) return; //阻止下次请求（false可以进行请求）；
      if (that.loadend) return; //阻止结束当前请求（false可以进行请求）；
      that.loading = true;
      getHostProducts(that.page, that.limit).then(res => {
        that.loading = false;
        let good_list = res.data;
        let goodArray = [];
        let count = Math.ceil(good_list.length / 4);
        for (let i = 0; i < count; i++) {
          var list = good_list.slice(i * 4, i * 4 + 4);
          if (list.length) goodArray.push({ list: list });
        }
        this.hostProduct = goodArray;
      });
    },
    morelike(){
        this.$router.push('/')
    },
    // 切换推荐列表
    prev() {
      this.$refs.carousel.prev();
    },
    next() {
      this.$refs.carousel.next();
    },
    // 商品详情跳转
    _goGoodsDetail(item) {
      this.$router.push({ path: "/detail/" + item.id});
    },
    // 显示商品信息
    _showGoodsInfo(item) {
      this.$refs.addCart.show(item.id,()=>{
        this.$emit('callback', 'reload')
      })
    }
  }
};
</script>

<style scoped  lang="scss" scoped>
$color: #DA213D;
$bgColor: #DA213D;
.recommend-warp {
  padding-top: 60px;
  padding-bottom: 60px;
  .tit {
    height: 48px;
    background-color: #f3f3f3;
    padding: 0 20px;
    font-size: 16px;
    color: #333;
    margin-bottom: 40px;
    img {
      display: block;
      width: 16px;
      height: 16px;
      object-fit: contain;
      margin-right: 10px;
    }
    .more {
      font-size: 14px;
      color: #999;
    }
  }
}
.slide {
  padding: 0 40px;
}
.recommend-box {
  height: 360px;
  box-sizing: border-box;
  margin-top: 40px;
  margin-bottom: 60px;
  .tit {
    font-size: 16px;
    color: #999;
    margin-bottom: 10px;
    .en {
      color: #333;
      font-size: 20px;
      font-weight: 600;
      margin-right: 40px;
    }
  }
  .el-carousel__arrow--left {
    left: 0px;
  }
  .el-carousel__arrow--right {
    right: 0px;
  }
  .el-carousel__arrow {
    background: none;
    cursor: pointer;
    top: 130px;
    i {
      font-size: 30px;
      color: #666;
    }
  }
  .el-carousel__indicators--horizontal {
    display: none;
  }
}

.goods-recommend {
  .goods-box {
    box-sizing: border-box;
    width: 260px;
  }
  .goods-img {
    width: 100%;
    height: 260px;
    display: block;
    cursor: pointer;
  }
  .goods-name {
    font-size: 14px;
    color: #333;
    height: 30px;
    line-height: 30px;
    margin-top: 10px;
    cursor: pointer;
  }
  .goods-price {
    margin-top: 10px;
    span {
      font-size: 14px;
      font-weight: 600;
      color: $color;
    }
    img {
      display: block;
      width: 20px;
      height: 20px;
      cursor: pointer;
    }
  }
}
</style>
